package com.vehicle.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.bean.DataGridView;
import com.vehicle.mapper.DriverMapper;
import com.vehicle.pojo.Driver;
import com.vehicle.service.DriverService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DriverServiceImp implements DriverService {
    @Autowired
    private DriverMapper driverMapper;

    @Transactional(
        propagation = Propagation.NOT_SUPPORTED
    )
    public DataGridView findAllDriver(Driver driver) {
        Page<Object> page = PageHelper.startPage(driver.getPage(), driver.getLimit());
        List<Driver> driverList = this.driverMapper.selectDriversByPage(driver);
        DataGridView dataGridView = new DataGridView();
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(driverList);
        return dataGridView;
    }

    @Transactional
    @Override
    public void addDriver(Driver driver) {
        driverMapper.insertDriver(driver);
    }

    //没车的driver
    @Override
    public List<Driver> noCar() {
        return driverMapper.findDriverWithNoCar();
    }

    //修改驾驶员
    @Override
    public void editDriver(Driver driver) {
        driverMapper.updateDriver(driver);
    }

    //修改状态删除
    @Override
    public void updateStatus(Integer driverid) {
        driverMapper.updateStatus(driverid);
    }

    //批量删除
    @Override
    public void deleteBatch(Integer[] driverids) {
        if (driverids!=null &&driverids.length>0){
            for (Integer driverid:driverids
                 ) {
                driverMapper.updateStatus(driverid);
            }
        }
    }
}
